package com.zxdemo.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.mysql.fabric.xmlrpc.base.Array;
import com.zxdemo.jpa.model.Role;
import com.zxdemo.jpa.model.User;
import com.zxdemo.service.RoleService;
import com.zxdemo.service.UserRoleService;
import com.zxdemo.service.UserService;

@RestController
public class UserController {
	 @Autowired private UserService userService;
	 @Autowired private RoleService roleService;
	 @Autowired private UserRoleService userRoleService;
	 @RequestMapping("/getuser")
		public JSONObject getUser(@RequestParam(value="page") int page,
				                  @RequestParam(value="rows") int rows)
		{
		 	int start=(page-1)*rows;
		 	int total=userService.queryTotal();
	    	List<User> list =userService.queryList(start,rows);
	    	JSONObject jsonObject=new JSONObject();
	    	jsonObject.put("total", total);
	    	jsonObject.put("rows", list);
	    	return jsonObject;
		}
	@RequestMapping("/adduser")
		public String addUser(@RequestParam(value="username") String userName,
				              @RequestParam(value="password") String password,
				              @RequestParam(value="tel") String tel,
				              @RequestParam(value="status") int status,
				              @RequestParam(value="role") String role)
							 
		{
		 User user=new User();
		 user.setUserName(userName);
		 user.setUserPassword(password);
		 user.setMobile(tel);
		 user.setStatus(status);
		 List<Long> roleList=new ArrayList<Long>();	
		 roleList.add(Long.parseLong(role));
		 user.setRoleIdList(roleList);
		 userService.save(user);
	     return "添加成功";
		}
	 @RequestMapping("/deluser")
		public String getUser(@RequestParam(value="id") long id)
		{
		 	System.out.println(id);
		 	userService.deleteByUserId(id);
		 	userRoleService.delete(id);
	    	return "删除成功";
		}
	
	
	
	 //新建用户 获取角色信息
	 @RequestMapping("/formgetrole")
	 	public List<Role> formgetrole()
	 	{
		 	List<Role> list = roleService.queryList(0, 100);
		 	return list;
	 	}
}
